//
//  coupleLiveAPI.m
//  theCoupleLive
//
//  Created by USER on 2016/12/3.
//  Copyright © 2016年 USER. All rights reserved.
//

#import "coupleLiveAPI.h"
#import "JSONKit.h"
#import "AFNetworking.h"
#import "Header.h"

@implementation coupleLiveAPI
/**
 注册
 
 @param name       用户名
 @param pwd        密码
 @param p_img     头像
 @param p_birth    生日
 @param p_address  地区
 @param p_sex      性别
 @param p_nickname 昵称
 @param success    返回值
 */
+(void)registLiveWithName:(NSString *)name pwd:(NSString *)pwd p_img:(NSString *)p_img p_birth:(NSString *)p_birth p_address:(NSString *)p_address p_sex:(NSString *)p_sex p_nickname:(NSString *)p_nickname success:(void (^)(id response))success
{
    // 1.创建请求
    NSURL *url = [NSURL URLWithString:registAPI];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    
    // 2.设置请求头
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    // 3.设置请求体
    NSDictionary *json = @{
                           @"name" :name,
                           @"password" : pwd,
                           @"p_img" : p_img,
                           @"p_birth" : p_birth,
                           @"p_address" : p_address,
                           @"p_sex" : p_sex,
                           @"p_nickname" : p_nickname
                           };
    NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
    request.HTTPBody = data;
    
    // 4.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        //data转字典
        NSString *str=[[NSString alloc]initWithData:data encoding:kCFStringEncodingUTF8];
        NSDictionary *data2 = [str objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        success(data2);
    }];
}


/**
 短信验证
 
 @param mobile  电话号码
 @param content 内容
 @param success 返回值
 */
+(void)getNoteWithMobile:(NSString *)mobile content:(NSString *)content success:(void (^)(id response))success{
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
    NSDictionary *dic=@{
                        @"account":account,
                        @"password":password,
                        @"mobile":mobile,
                        @"content":content
                        };
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [manager POST:noteAPI parameters:dic constructingBodyWithBlock:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //        NSLog(@"%@",error);
    }];
}


/**
 登录

 @param name      电话号码
 @param pwd       密码
 @param u_address 地址
 @param u_gps     经纬度
 @param success   返回值
 */
+(void)loginLiveWithName:(NSString *)name pwd:(NSString *)pwd u_address:(NSString *)u_address u_gps:(NSString*)u_gps success:(void (^)(id response))success
{
    // 1.创建请求
    NSURL *url = [NSURL URLWithString:loginAPI];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    
    // 2.设置请求头
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//    NSLog(@"%@",u_gps);
    // 3.设置请求体
    NSDictionary *json = @{
                           @"name" :name,
                           @"password" : pwd,
                           @"u_address" : u_address,
                           @"u_gps" :u_gps
                           };
    NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
    request.HTTPBody = data;
    
    // 4.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        //data转字典
        NSString *str=[[NSString alloc]initWithData:data encoding:kCFStringEncodingUTF8];
        NSDictionary *data2 = [str objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        success(data2);
    }];
}

/**
 忘记密码
 
 @param name      电话号码
 @param pwd       new密码
 @param success   返回值
 */
+(void)forgetPwdLiveWithName:(NSString *)name pwd:(NSString *)pwd success:(void (^)(id response))success
{
    // 1.创建请求
    NSURL *url = [NSURL URLWithString:forgetPwdAPI];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    
    // 2.设置请求头
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    // 3.设置请求体
    NSDictionary *json = @{
                           @"name" :name,
                           @"password" : pwd
                           };
    NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
    request.HTTPBody = data;
    
    // 4.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        //data转字典
        NSString *str=[[NSString alloc]initWithData:data encoding:kCFStringEncodingUTF8];
        NSDictionary *data2 = [str objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        success(data2);
    }];
}

/**
 附近人

 @param uid     用户id
 @param gps     用户位置
 @param success 返回值
 */
+(void)nearbyPeopleWithuid:(NSString *)uid ugps:(NSString *)gps success:(void (^)(id response))success
{
    // 1.创建请求
    NSURL *url = [NSURL URLWithString:nearbyPeopleAPI];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    
    // 2.设置请求头
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    // 3.设置请求体
    NSDictionary *json = @{
                           @"uid" :uid,
                           @"u_gps":gps
                           };
    NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
    request.HTTPBody = data;
    
    // 4.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        //data转字典
        NSString *str=[[NSString alloc]initWithData:data encoding:kCFStringEncodingUTF8];
        NSMutableArray *data2 = [str objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
//        NSLog(@"%@",[[data2 objectAtIndex:0]objectForKey:@"km"]);
        success(data2);
    }];
}


/**
 附近的群组

 @param uid     用户id
 @param gps     用户位置
 @param success 返回值
 */
+(void)nearbyGroupWithuid:(NSString *)uid ugps:(NSString *)gps success:(void (^)(id response))success
{
    // 1.创建请求
    NSURL *url = [NSURL URLWithString:nearbyGroupAPI];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    
    // 2.设置请求头
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    // 3.设置请求体
    NSDictionary *json = @{
                           @"uid" :uid,
                           @"u_gps":gps
                           };
    NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
    request.HTTPBody = data;
    
    // 4.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        //data转字典
        NSString *str=[[NSString alloc]initWithData:data encoding:kCFStringEncodingUTF8];
        NSMutableArray *data2 = [str objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
        success(data2);
    }];
}

+(void)nearbyDynamicWithuid:(NSString *)uid ugps:(NSString *)gps address:(NSString *)address success:(void (^)(id response))success
{
    // 1.创建请求
    NSURL *url = [NSURL URLWithString:nearbyDynamciAPI];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    
    // 2.设置请求头
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    // 3.设置请求体
    NSDictionary *json = @{
                           @"uid" :uid,
                           @"gps":gps,
                           @"address":address
                           };
    NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
    request.HTTPBody = data;
    
    // 4.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        //data转字典
        NSArray *arr = [NSJSONSerialization JSONObjectWithData:data
                                                            options:NSJSONReadingMutableContainers
                                                              error:nil];
        success(arr);
    }];
}

@end
